//
//  RegistViewController.m
//  Comic
//
//  Created by Andy.He on 16/3/17.
//  Copyright © 2016年 程锦. All rights reserved.
//

#import "RegistViewController.h"
#import "HAChatViewController.h"
#import "UserRegistViewController.h"

@interface RegistViewController () <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *userNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *passportTextField;

@property (strong, nonatomic) IBOutlet UIButton *loginBtn;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *leftConsLog;

@property (strong, nonatomic) IBOutlet UIButton *registBtn;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *leftConsReg;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@property (strong, nonatomic) IBOutlet UILabel *attentLabel;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *labelCons;

@property (strong, nonatomic) IBOutlet UIView *textView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *viewConsLeft;

@property (strong, nonatomic) UIAlertController *alertController;
@property (strong, nonatomic) IBOutlet UIButton *jumpBtn;

@property (strong, nonatomic) NSString *username;
@property (strong, nonatomic) NSString *passport;

@end

@implementation RegistViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.leftConsReg.constant = SCREEN_WIDTH / 2 - self.registBtn.frame.size.width / 2;
    self.leftConsLog.constant = SCREEN_WIDTH / 2 - self.loginBtn.frame.size.width / 2;
    self.labelCons.constant = SCREEN_WIDTH / 2 - self.attentLabel.frame.size.width / 2;
    self.viewConsLeft.constant = SCREEN_WIDTH / 2 - self.textView.frame.size.width / 2;
    
    self.userNameTextField.delegate = self;
    self.passportTextField.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(infoAction)name:UITextFieldTextDidChangeNotification object:nil];
    
    self.textView.layer.cornerRadius = 15;

    [self btnSetting];
    [self imgSetting];
    
}

- (void)btnSetting{
    self.loginBtn.layer.borderWidth = 1;
    self.loginBtn.layer.cornerRadius = 10;
    self.loginBtn.layer.masksToBounds = YES;
    
    self.registBtn.layer.borderWidth = 1;
    self.registBtn.layer.cornerRadius = 10;
    self.loginBtn.layer.masksToBounds = YES;
    [self.loginBtn addTarget:self action:@selector(dismissBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.registBtn addTarget:self action:@selector(registView) forControlEvents:UIControlEventTouchUpInside];
    [self.jumpBtn addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
}

- (void)backToView{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)registView{
    UserRegistViewController *usrReg = [[UserRegistViewController alloc] initWithNibName:NSStringFromClass([UserRegistViewController class]) bundle:nil];
    usrReg.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:usrReg animated:YES completion:nil];
    
}

- (void)alertSetting:(NSString *)message{
    self.alertController = [UIAlertController alertControllerWithTitle:@"提醒" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
    }];
    [self.alertController addAction:action];
    
    [self presentViewController:self.alertController animated:YES completion:nil];
}

- (void)dismissBtn{
    [SVProgressHUD showWithStatus:@"别急,来啦..."];
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.username password:self.passport completion:^(NSDictionary *loginInfo, EMError *error) {
        NSLog(@"-----%@",loginInfo);
        if (!error && loginInfo) {
            NSLog(@"登陆成功");
            [SVProgressHUD showSuccessWithStatus:@"哎呀,你登录上了"];
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            [SVProgressHUD showErrorWithStatus:@"喂,你账号密码输错了!"];
//            [self alertSetting:@"账号或密码错误,请重新输入"];
        }
    } onQueue:nil];

}

- (void)infoAction{
    self.username = self.userNameTextField.text;
    self.passport = self.passportTextField.text;
}

- (void)imgSetting{
    self.imageView.image = [UIImage imageNamed:@"user"];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.passportTextField) {
        self.imageView.image = [UIImage imageNamed:@"passport"];
    }else{
        self.imageView.image = [UIImage imageNamed:@"user"];

    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.userNameTextField resignFirstResponder];
    [self.passportTextField resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
